<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CORS修复测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        .test-section {
            margin: 20px 0;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 8px;
        }
        button {
            background: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 4px;
            cursor: pointer;
            margin: 5px;
        }
        button:hover {
            background: #0056b3;
        }
        .result {
            margin-top: 10px;
            padding: 10px;
            background: #f8f9fa;
            border-radius: 4px;
            white-space: pre-wrap;
        }
        .error {
            background: #f8d7da;
            color: #721c24;
        }
        .success {
            background: #d4edda;
            color: #155724;
        }
    </style>
</head>
<body>
    <h1>CORS修复测试页面</h1>
    
    <div class="test-section">
        <h2>1. 测试项目列表API (GET)</h2>
        <button onclick="testGetProjects()">测试获取项目列表</button>
        <div id="get-result" class="result"></div>
    </div>
    
    <div class="test-section">
        <h2>2. 测试创建项目API (POST)</h2>
        <button onclick="testCreateProject()">测试创建项目</button>
        <div id="post-result" class="result"></div>
    </div>
    
    <div class="test-section">
        <h2>3. 配置说明</h2>
        <p><strong>已应用的CORS解决方案：</strong></p>
        <ul>
            <li>✅ 配置了Vite开发服务器代理</li>
            <li>✅ 修改了API基础URL配置</li>
            <li>✅ 修复了TypeScript类型问题</li>
        </ul>
        
        <p><strong>代理配置详情：</strong></p>
        <pre>
// vite.config.ts
server: {
  proxy: {
    '/api': {
      target: 'http://localhost:5000',
      changeOrigin: true,
      secure: false
    }
  }
}
        </pre>
        
        <p><strong>使用方法：</strong></p>
        <ol>
            <li>确保后端服务运行在 http://localhost:5000</li>
            <li>重启前端开发服务器: <code>npm run dev</code></li>
            <li>现在所有API请求都会通过代理转发，避免CORS问题</li>
        </ol>
    </div>

    <script>
        // 模拟API请求函数
        async function makeRequest(url, options = {}) {
            try {
                const response = await fetch(url, {
                    headers: {
                        'Content-Type': 'application/json',
                        ...options.headers
                    },
                    ...options
                });
                
                const data = await response.json();
                return { success: true, data, status: response.status };
            } catch (error) {
                return { success: false, error: error.message };
            }
        }
        
        async function testGetProjects() {
            const resultDiv = document.getElementById('get-result');
            resultDiv.textContent = '正在测试...';
            resultDiv.className = 'result';
            
            const result = await makeRequest('/api/v1/projects/list');
            
            if (result.success) {
                resultDiv.textContent = `✅ GET请求成功!\n状态码: ${result.status}\n响应数据: ${JSON.stringify(result.data, null, 2)}`;
                resultDiv.className = 'result success';
            } else {
                resultDiv.textContent = `❌ GET请求失败!\n错误信息: ${result.error}`;
                resultDiv.className = 'result error';
            }
        }
        
        async function testCreateProject() {
            const resultDiv = document.getElementById('post-result');
            resultDiv.textContent = '正在测试...';
            resultDiv.className = 'result';
            
            const testData = {
                project_code: `TEST-${Date.now()}`,
                customer_name: '测试客户',
                customer_domain: 'test.com',
                project_type: 'BACKLINK',
                status: 'PENDING',
                contract_terms: {
                    text: '测试合同',
                    amount: 1000
                },
                contract_requirements: []
            };
            
            const result = await makeRequest('/api/v1/projects', {
                method: 'POST',
                body: JSON.stringify(testData)
            });
            
            if (result.success) {
                resultDiv.textContent = `✅ POST请求成功!\n状态码: ${result.status}\n响应数据: ${JSON.stringify(result.data, null, 2)}`;
                resultDiv.className = 'result success';
            } else {
                resultDiv.textContent = `❌ POST请求失败!\n错误信息: ${result.error}`;
                resultDiv.className = 'result error';
            }
        }
    </script>
</body>
</html>